package ee.smkv.Badminton_score_counter;

import android.os.Bundle;
import android.util.Log;
import com.sonyericsson.extras.liveware.aef.control.Control;
import com.sonyericsson.extras.liveware.extension.util.control.ControlExtension;
import com.sonyericsson.extras.liveware.extension.util.control.ControlObjectClickEvent;


public class BadmintonScoreCounterControlExtension extends ControlExtension {

    private static final int MENU_ITEM_NEW = 1;
    private static final int MENU_ITEM_UNDO = 2;
    private Game game = new Game();
    Bundle[] mMenuItemsText = new Bundle[2];


    public BadmintonScoreCounterControlExtension(ReceiverService service, String hostAppPackageName) {
        super(service, hostAppPackageName);

        mMenuItemsText[0] = new Bundle();
        mMenuItemsText[0].putInt(Control.Intents.EXTRA_MENU_ITEM_ID, MENU_ITEM_NEW);
        mMenuItemsText[0].putString(Control.Intents.EXTRA_MENU_ITEM_TEXT, "New");

        mMenuItemsText[1] = new Bundle();
        mMenuItemsText[1].putInt(Control.Intents.EXTRA_MENU_ITEM_ID, MENU_ITEM_UNDO);
        mMenuItemsText[1].putString(Control.Intents.EXTRA_MENU_ITEM_TEXT, "Undo");


    }

    @Override
    public void onResume() {
        super.onResume();
        newGame();
    }

    private void newGame() {
        game = new Game();
        showLayout(R.layout.game, null);
        updateGameScore();
    }

    private void updateGameScore() {
        if (game.isGameOver()) {
            showLayout(R.layout.gameover, null);
            sendText(R.id.WinnerDetails, mContext.getString(game.isIWon() ? R.string.game_over_you_won : R.string.game_over_opponent_won));
            startVibrator(500,0,0);
        } else {
            sendText(R.id.MyScoreTextView, String.format("%s", game.getMyScore()));
            sendText(R.id.OponentScoreTextView, String.format("%s", game.getOpponentScore()));
            sendImage(R.id.MyTurnImage, game.isMyTurn() ? R.drawable.badminton : R.drawable.blank);
            sendImage(R.id.OponentTurnImage, game.isOpponentTurn() ? R.drawable.badminton : R.drawable.blank);
        }
    }

    @Override
    public void onKey(final int action, final int keyCode, final long timeStamp) {
        if (action == Control.Intents.KEY_ACTION_RELEASE
                && keyCode == Control.KeyCodes.KEYCODE_OPTIONS) {
            showMenu(mMenuItemsText);
        }
    }


    @Override
    public void onObjectClick(final ControlObjectClickEvent event) {
        switch (event.getLayoutReference()) {
            case R.id.MyGameField:
                game.addPointToMe();
                updateGameScore();
                break;

            case R.id.OponentGameField:
                game.addPointToOpponent();
                updateGameScore();
                break;

            case R.id.GameOverDialog:
                newGame();
                break;
        }
    }

    @Override
    public void onMenuItemSelected(final int menuItem) {
        if (menuItem == MENU_ITEM_NEW) {
            newGame();
        }
        if (menuItem == MENU_ITEM_UNDO) {
            game.undo();
            updateGameScore();
        }
    }

}
